package com.cqrt.controller;



import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.dto.ProjectPlanChangeDTO;
import com.cqrt.dto.RTPlanUpdateDTO;
import com.cqrt.service.RTOaChangeInfoService;

import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.hustcad.plm.rpm.project.service.TyppmProjectService;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.session.TyAccountContext;
import com.ty.ppm.model.constant.ApiPathConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;


@RestController
@Api(tags = "OA集成")
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "oaApi")
public class RTOaController {

    @Resource
    private RTOaChangeInfoService rtOaChangeInfoService;

    @Resource
    private TyppmProjectService typpmProjectService;

    @Resource
    private TyplmUserService typlmUserService;

    @Value(value = "${rato_oabot}")
    private String OABot;

    @PostMapping("/changePlan")
    @ApiOperation(value = "项目计划暂停", produces = "application/json; charset=utf-8")
    public ResponseResult<String> changePlan(@RequestParam(value = "projectId")String projectId,
                                             @RequestParam(value = "type")String type) {
        String URL = rtOaChangeInfoService.changePlan(projectId,type);
        if (StrUtil.isBlank(URL)){
            return ResponseResult.fail("400","失败，OA返回链接为空");
        }
        return ResponseResult.success(URL);
    }

    @PostMapping("/changePlan1")
    @ApiOperation(value = "更改项目计划下发OA", produces = "application/json; charset=utf-8")
    public ResponseResult<String> changePlan1(@RequestBody ProjectPlanChangeDTO projectPlanChangeDTO) {
        String URL = "";
        try {
            URL = rtOaChangeInfoService.changePlan1(projectPlanChangeDTO);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }
        return ResponseResult.success(URL);
    }


    @PostMapping("/pause")
    public ResponseResult<String> pause(@RequestBody String oid){
        String string = JSONObject.parseObject(oid).getString("oid");
        UserDO userDO = typlmUserService.queryUserByKey(new BigInteger(OABot));
        TyAccountContext.setUser(userDO);
        typpmProjectService.setProjectStatePaused(Arrays.asList(string));
        return ResponseResult.success();
    }

    @PostMapping("/restart")
    public ResponseResult<String> restart(@RequestBody String oid){
        String string = JSONObject.parseObject(oid).getString("oid");
        UserDO userDO = typlmUserService.queryUserByKey(new BigInteger(OABot));
        TyAccountContext.setUser(userDO);
        typpmProjectService.setProjectStateRecovery(Arrays.asList(string));
        return ResponseResult.success();
    }

    @PostMapping("/updatePlanDate")
    public ResponseResult<String> updatePlanDate(@RequestBody JSONArray jsonArray){
        List<RTPlanUpdateDTO> rtPlanUpdateDTOS =jsonArray.toJavaList(RTPlanUpdateDTO.class);
        try {
            UserDO userDO = typlmUserService.queryUserByKey(new BigInteger(OABot));
            TyAccountContext.setUser(userDO);
            rtOaChangeInfoService.updatePlanDate(rtPlanUpdateDTOS);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }
        return ResponseResult.success();
    }
    @PostMapping("/updatePlanActivity")
    public ResponseResult<String> updatePlanActivity(@RequestBody String text){
        JSONObject jsonObject = JSONObject.parseObject(text);
        try {
            UserDO userDO = typlmUserService.queryUserByKey(new BigInteger(OABot));
            TyAccountContext.setUser(userDO);
            rtOaChangeInfoService.updatePlanActivity(jsonObject.getString("key"),jsonObject.getString("projectId"));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.fail("400",e.getMessage());
        }
        return ResponseResult.success();
    }
}
